// Copyright 2025 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package internal

import (
	"time"

	"github.com/juju/juju/core/machine"
	coreoperation "github.com/juju/juju/core/operation"
	corestatus "github.com/juju/juju/core/status"
	"github.com/juju/juju/core/unit"
)

// TaskLogMessage is a timestamped message logged for a task.
type TaskLogMessage struct {
	Message   string
	Timestamp time.Time
}

// TransformToCore returns the log message in core operation format.
func (t TaskLogMessage) TransformToCore() coreoperation.TaskLogMessage {
	return coreoperation.TaskLogMessage{
		Message:   t.Message,
		Timestamp: t.Timestamp,
	}
}

// CompletedTask is the data required to finish a task.
type CompletedTask struct {
	TaskUUID  string
	StorePath string
	Status    string
	Message   string
}

// ImportOperationsArgs are the arguments for ImportOperations during model migration.
// Each operation preserves its natural numeric/string ID and carries its tasks.
// UUIDs are currently generated by the target model; fields are present for a future
// UUID migration but should be empty for now.
type ImportOperationsArgs []ImportOperationArg

type ImportOperationArg struct {
	ID        string
	Summary   string
	Enqueued  time.Time
	Started   time.Time
	Completed time.Time
	Status    corestatus.Status
	Fail      string
	Tasks     []ImportTaskArg

	// Should be the same for all tasks in the operation.
	IsParallel     bool
	ExecutionGroup string
	Parameters     map[string]any
	Application    string // can be empty for machine operations
	ActionName     string // ignored for machine operations (will be empty or `juju-exec`)

	// Future: optional UUID if we later migrate UUIDs too; empty -> generate.
	UUID string
}

// ImportTaskArg are the arguments for ImportTask during model migration.
// Each task preserves its natural numeric/string ID and carries its logs.
// UUIDs are currently generated by the target model; fields are present for a future
// UUID migration but should be empty for now.
type ImportTaskArg struct {
	ID          string
	Enqueued    time.Time
	Started     time.Time
	Completed   time.Time
	Status      corestatus.Status
	Message     string
	Output      map[string]any
	Log         []TaskLogMessage
	UnitName    unit.Name
	MachineName machine.Name

	// Generated in service layer
	StorePath string

	// Future: optional UUID; empty -> generate.
	UUID string
}

// ReceiversWithResolvedLeaders represents receivers with matched leaders.
type ReceiversWithResolvedLeaders struct {
	Applications []string
	Machines     []machine.Name
	Units        []unit.Name
	LeaderUnits  []unit.Name
}
